package ren.kane.phoenix.util.io;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Scanner;

/**
 * @author renrk(1275) on 2019-08-06 3:17 PM
 **/
public class BufferedOutputStreamTest {

    private static final int LEN = 5;
    // 对应英文字母“abcddefghijklmnopqrsttuvwxyz”
    private static final byte[] ARRAY_LETTERS = {
        0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
        0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A
    };

    // 创建“文件输出流”对应的BufferedOutputStream，它对应缓冲区的大小是16，即缓冲区的数据>=16时，会自动将缓冲区的内容写入到输出流
    private static void testBufferedOutputStream() {
        try {
            File file = new File("dir", "out.txt");
            OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file), 16);
            // 将ArrayLetters数组的前10个字节写入到输出流中
            outputStream.write(ARRAY_LETTERS, 0, 10);
            // outputStream.write(ARRAY_LETTERS, 0, 20);
            // 将“换行符\n”写入到输出流中
            outputStream.write('\n');
            // TODO!
            //out.flush();
            // 读取用户输入
            readUserInput();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void readUserInput() {
        System.out.println("please input a text:");
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        System.out.printf("the input is : %s\n", str);
    }

    public static void main(String[] args) {
        testBufferedOutputStream();
    }
}
